﻿using System;


class BinarySearch
{
    static void Main()
    {
        Console.Write("How long will be your array : ");
        int n = int.Parse(Console.ReadLine());
        int[] MyArray = new int[n];
        Console.WriteLine("Insert {0} integer", n);
        for (int i = 0; i < MyArray.Length; i++)
        {
            Console.Write("{0}:", i + 1);
            MyArray[i] = int.Parse(Console.ReadLine());
        }
        Array.Sort(MyArray);
        Console.Write("Insert searches number - ");
        int Search = int.Parse(Console.ReadLine());
        int MinArrayIndex = 0;
        int MaxArrayIndex = MyArray.Length - 1;
        int MidPoint = new int();
        while (MinArrayIndex <= MaxArrayIndex)
        {
            MidPoint = (MinArrayIndex + MaxArrayIndex) / 2;

            if (MyArray[MidPoint] == Search)
            {
                Console.WriteLine("Number {0} in this array have index {1}" , Search , MidPoint);
                break;
            }
            else if (Search < MyArray[MidPoint])
            {
                MaxArrayIndex = MidPoint - 1;
            }
            else
            {
                MinArrayIndex = MidPoint + 1;
            }

        }
        if (MinArrayIndex > MaxArrayIndex)
        {
            Console.WriteLine("Searched element not belong to this array");
        }
    }
}
